package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mukundd on 28/02/14.
 */
public class Library {
    private List<Book> books = new ArrayList<Book>();
    private List<Customer> customers = new ArrayList<Customer>();
    private List<Movie> movies = new ArrayList<Movie>();

    public Library(){
        books.add(new Book("1","Book 1"));
        books.add(new Book("2","Book 2"));

        customers.add(new Customer("111-1111","password1","Customer 1","cus1@gmail.com","1111111111"));
        customers.add(new Customer("222-2222","password2","Customer 2","cus2@gmail.com","2222222222"));

        movies.add(new Movie("Movie 1",2000,"Director 1",8));
        movies.add(new Movie("Movie 2",2002,"Director 1",9));

    }



    public void setBooks(Book book) {
        books.add(book);
    }

    public List<Book> getBooks() {

        return books;
    }

    public List<Book> listBooks (){
        return books;
    }

    public String checkOutBook(String customerBorrowing, String bookToBorrow){
        for(Customer customer :customers){
            if(customer.getName().equals(customerBorrowing) ){
                for (Book book:books){
                    if(book.Name.equals(bookToBorrow)){
                        customer.booksBorrowed.add(book);
                        books.remove(book);
                        return ("Thank you! Enjoy the book");
                    }
                }
                return ("That book is not available");
            }
        }
    return null;
    }

    public String returnBook(String customerReturning, String bookToReturn){
        for(Customer customer :customers){
            if(customer.getName().equals(customerReturning) ){
                for (Book book:customer.booksBorrowed){
                    if(book.Name.equals(bookToReturn)){
                        books.add(book);
                        customer.booksBorrowed.remove(book);
                        return ("Thank you for returning the book.");
                    }
                }
                return ("That is not a valid book to return.");
            }
        }
        return null;
    }

    public List<Movie> listMovies (){
        return movies;
    }

    public String checkOutMovie(String customerBorrowing, String movieToBorrow){
        for(Customer customer :customers){
            if(customer.getName().equals(customerBorrowing) ){
                for (Movie movie:movies){
                    if(movie.getName().equals(movieToBorrow)){
                        customer.moviesBorrowed.add(movie);
                        movies.remove(movie);
                        return ("Thank you! Enjoy the Movie");
                    }
                }
                return ("That Movie is not available");
            }
        }
        return null;
    }

    public Customer AuthenticateUser(String id,String password){
        Customer LoggedInUser=null;
        for(Customer customer: customers){
            if(customer.getId().equals(id)&&customer.getPassword().equals(password)){
                LoggedInUser=customer;
                break;
            }
        }
        return LoggedInUser;
    }

}
